
#include "CondC.h"
#include "Common/TableDataEx/DuplicateCfg.h"


CondC::CondC() :m_pDuplicate(nullptr), m_pVecCond(nullptr), m_result(-1), m_finish(false)
{
	m_setType.clear();
}

CondC::~CondC()
{

}

bool CondC::init(IDuplicate *pDuplicate, const VEC_INT32 *pCond, const string &strSubCond)
{
	if (nullptr == pDuplicate || nullptr == pCond)
	{
		return false;
	}
	//3，塔的ID
	int32_t isize = pCond->size();
	if (isize < 2)
	{
		LogErrFmtPrint("[logic] CondC::init....isize < 2, dupid:%u, sceneid:%u , isize:%d ", pDuplicate->GetDuplicateID(), pDuplicate->GetSceneID(), isize);
		return false;
	}
	m_pVecCond = pCond;
	m_pDuplicate = pDuplicate;
	//塔ID
	m_towerId = m_pVecCond->at(1);
	//解析怪物序列
	CommonApi::SplitStrToSetInt(strSubCond, ",", &m_setType);
	//移除类型为0的
	SET_INT32::iterator iterdel = m_setType.find(0);
	if (iterdel != m_setType.end())
	{
		m_setType.erase(iterdel);
	}

	return true;
}

void CondC::uninit()
{
	m_pDuplicate = nullptr;
	m_pVecCond = nullptr;
	m_finish = false;
	m_result = -1;
}

bool CondC::finishCond()
{
	return m_finish;
}

int32_t CondC::result()
{
	return m_result;
}

uint32_t CondC::towerMonsId()
{
	return m_towerId;
}

void CondC::onMonsHpUpdate(int32_t hp)
{
	if (nullptr != m_pDuplicate)
	{
		Duplicate_TowerHpRsp rsp;
		rsp.set_hp(hp);
		m_pDuplicate->BroadCastData(EMODULE_ID_DUPLICATE, LOGOC_TO_CLIENT_DUPLICATE_TOWER_HP, &rsp);
	}
}

void CondC::condUpdate()
{
	if (m_finish)
	{
		return;
	}
	m_result = checkCond();
	if (RET_SUCCESS == m_result || RET_FAIL == m_result)
	{
		m_finish = true;
	}
}

int32_t CondC::checkCond()
{
	if (nullptr == m_pVecCond || nullptr == m_pDuplicate)
	{
		return -1;
	}
	/*
	1，指定时间内消灭指定怪物序列，才能通关；超时失败
	2，指定时间内消灭所有怪物，才能通关；超时失败
	3，消灭所有脚本刷新怪物，才能通关；塔被怪物消灭失败
	4，达到指定时间点，怪物传送的数量小于目标值；怪物传送的数量达到目标值，或有BOSS进行传送，失败

	1，限制时间，怪物1的ID，怪物2的ID，……
	2，限制时间
	3，塔的ID
	4，限制时间，传送区域ID，目标值，BOSS1的ID，BOSS2的ID，……
	*/
	int32_t ret = -1;
	//塔死亡，塔也是当做一种怪
	if (m_pDuplicate->GetDieMonsterNum(m_towerId) > 0)
	{
		ret = RET_FAIL; //塔死亡，失败
	}
	else if (m_pDuplicate->IsBrushMonsAllDie())
	{
		ret = RET_SUCCESS; //所有脚本刷出来的怪物全部死亡,成功
	}

	return ret;
}
